﻿#include "infoadmin.h"

adminInfo::adminInfo()
{
    //adminInfo构造函数就设置它的
    this->setM=new modelSet;
    this->op=new fileOperator;
}

void adminInfo::displayAllStudent(QStandardItemModel *model)
{
    model->clear();
    for(int i=0;i<stuInfo.length();i++)
    {
        this->stuInfo.at(i).checkMyInfo(model,i);
    }
}

void adminInfo::displayAllTeacher(QStandardItemModel *model)
{
    model->clear();
    for(int i=0;i<teaInfo.length();i++)
    {
        this->teaInfo.at(i).checkMyInfo(model,i);
    }
}

void adminInfo::displayAllCourse(QStandardItemModel *model)
{
    model->clear();
    this->setM->setCourseTitle(model);
    for(int i=0;i<couInfo.length();i++)
    {
        this->couInfo.at(i).displayThisCourse(model,i);
    }
}

void adminInfo::addNewCourse(courseInfo &newCourse)
{
    couInfo.push_back(newCourse);
}

void adminInfo::addNewStudent(studentInfo &newStudent)
{
    stuInfo.push_back(newStudent);
}

void adminInfo::addNewTeacher(teacherInfo &newTeacher)
{
    teaInfo.push_back(newTeacher);
}

bool adminInfo::modifyPassword(QString oldPassword, QString newPassword)
{
    if(oldPassword==this->adminPassword)
    {
        if(newPassword.length()<6)
        {
            return false;
        }
        else
        {
            this->adminPassword=newPassword;
            return  true;
        }
    }
    else
    {
        return false;
    }
}

void adminInfo::checkMyInfo(QStandardItemModel *model)
{
    QStringList line;
    line.append(this->getName());
    line.append(this->getId());
    this->setM->setStudentTitle(model);
    this->setM->setStudentItem(model,line,0);
}

void adminInfo::modifyCourseInfo(courseInfo &info, QStringList newInfo)
{
    info.setCourseInfo(newInfo.at(0),newInfo.at(1),newInfo.at(2),newInfo.at(3),newInfo.at(4),newInfo.at(5),newInfo.at(6),newInfo.at(7),newInfo.at(8),newInfo.at(9),newInfo.at(10),newInfo.at(11),"0");
}

studentInfo &adminInfo::findStudentWithId(const QString &id)
{
    for(int i=0;i<stuInfo.length();i++)
    {
        if(stuInfo.at(i).getId()==id)
        {
            return stuInfo[i];
        }
    }
}

studentInfo &adminInfo::findStudentWithName(const QString &name)
{
    for(int i=0;i<stuInfo.length();i++)
    {
        if(stuInfo.at(i).getName()==name)
        {
            return stuInfo[i];
        }
    }
}

courseInfo &adminInfo::findCourseWithId(const QString &id)
{
    for(int i=0;i<couInfo.length();i++)
    {
        if(couInfo.at(i).getCourseSelectId()==id)
        {
            return couInfo[i];
        }
    }
}

int adminInfo::whoAreYou(QString id)
{
    switch(id.length())
    {
    //课程
    case 5:
        return 0;
    case 7:
        return 1;
    case 10:
        return 2;
    }
}

void adminInfo::displayWaitToLoadCourse(QStandardItemModel *model)
{
    model->clear();
    this->setM->setCourseTitle(model);
    for(int i=0;i<waitToLoadCourse.length();i++)
    {
        this->waitToLoadCourse.at(i).displayThisCourse(model,i);
    }
}

QList<courseInfo> &adminInfo::getWaitToLoadCourse()
{
    return waitToLoadCourse;
}

void adminInfo::saveAllInfo()
{
    for(int i =0;i<stuInfo.length();i++)
    {
        this->op->writeInfoToFile("stuInfo",stuInfo.at(i).getAllInfo());
    }
    this->op->replaceFile("stuInfo","stuInfo_temp");
    for(int i =0;i<teaInfo.length();i++)
    {
        this->op->writeInfoToFile("teacherInfo",teaInfo.at(i).getAllInfo());
    }
    this->op->replaceFile("teacherInfo","teacherInfo_temp");
    for(int i =0;i<couInfo.length();i++)
    {
        this->op->writeInfoToFile("courseInfo",couInfo.at(i).getAllInfo());
    }
    this->op->replaceFile("courseInfo","courseInfo_temp");
    for(int i =0;i<waitToLoadCourse.length();i++)
    {
        this->op->writeInfoToFile("waitToLoadCourse",waitToLoadCourse.at(i).getAllInfo());
    }
    this->op->replaceFile("waitToLoadCourse","waitToLoadCourse_temp");
    this->op->modifyFile("adminInfo",getAllInfo());
}

QStringList adminInfo::getAllInfo()
{
    QStringList temp;
    temp.append(getName());
    temp.append(getId());
    temp.append(adminPassword);
    return temp;
}

void adminInfo::setInfo(QString name, QString id, QString password)
{
    this->personInfo::setInfo(name,id);
    this->adminPassword=password;
    //加载全校课程信息
    QStringList _line1;
    this->op->readFromInfoFile("courseInfo.csv",_line1);
    for(int i=0;i<_line1.length();i++)
    {
        QString line=_line1.at(i);
        line=line.trimmed();
        QStringList linesplit=line.split(",");
        courseInfo a;
        a.setCourseInfo(linesplit.at(0),linesplit.at(1),linesplit.at(2),linesplit.at(3),linesplit.at(4),linesplit.at(5),linesplit.at(6),linesplit.at(7),linesplit.at(8),linesplit.at(9),linesplit.at(10),linesplit.at(11),linesplit.at(12));
        couInfo.push_back(a);
    }
    //加载所有学生
    QStringList _line2;
    this->op->readFromInfoFile("stuInfo.csv",_line2);
    for(int i=0;i<_line2.length();i++)
    {
        QString line=_line2.at(i);
        QStringList selectedCourse;
        line=line.trimmed();
        QStringList linesplit=line.split(",");
        for(int i=7;i<linesplit.length();i++)
        {
            selectedCourse.append(linesplit.at(i));
        }
        studentInfo a;
        a.setInfo(linesplit.at(0),linesplit.at(1),linesplit.at(3),linesplit.at(4),linesplit.at(5),linesplit.at(6),linesplit.at(2),selectedCourse);
        stuInfo.push_back(a);
    }
    //加载所有老师
    QStringList _line3;
    this->op->readFromInfoFile("teacherInfo.csv",_line3);
    for(int i=0;i<_line3.length();i++)
    {
        QString line=_line3.at(i);
        QStringList myCourse;
        line=line.trimmed();
        QStringList linesplit=line.split(",");
        for(int i=5;i<linesplit.length();i++)
        {
            myCourse.append(linesplit.at(i));
        }
        teacherInfo a;
        a.setInfo(linesplit.at(0),linesplit.at(1),linesplit.at(2),linesplit.at(3),linesplit.at(4),myCourse);
        teaInfo.push_back(a);
    }
    //加载老师开课需求
    QStringList _line4;
    this->op->readFromInfoFile("waitToLoadCourse.csv",_line4);
    for(int i=0;i<_line4.length();i++)
    {
        QString line=_line4.at(i);
        line=line.trimmed();
        QStringList linesplit=line.split(",");
        courseInfo a;
        a.setCourseInfo(linesplit.at(0),linesplit.at(1),linesplit.at(2),linesplit.at(3),linesplit.at(4),linesplit.at(5),linesplit.at(6),linesplit.at(7),linesplit.at(8),linesplit.at(9),linesplit.at(10),linesplit.at(11),linesplit.at(12));
        waitToLoadCourse.push_back(a);
    }
}

bool adminInfo::displayCourseWithId(const QString &id, QStandardItemModel *model)
{
    int j=0;
    setM->setCourseTitle(model);
    for(int i=0;i<couInfo.length();i++)
    {
        if(couInfo.at(i).getCourseSelectId()==id)
        {
            couInfo.at(i).displayThisCourse(model,j++);
        }
    }
    return j;
}

bool adminInfo::displayCourseWithName(const QString &name, QStandardItemModel *model)
{
    int j=0;
    setM->setCourseTitle(model);
    for(int i=0;i<couInfo.length();i++)
    {
        if(couInfo.at(i).getCourseName()==name)
        {
            couInfo.at(i).displayThisCourse(model,j++);
        }
    }
    return j;
}

bool adminInfo::displayTeacherWithId(const QString &id, QStandardItemModel *model)
{
    int j=0;
    setM->setTeacherTitle(model);
    for(int i=0;i<teaInfo.length();i++)
    {
        if(teaInfo.at(i).getId()==id)
        {
            teaInfo.at(i).checkMyInfo(model,j++);
        }
    }
    return j;
}

bool adminInfo::displayTeacherWithName(const QString &name, QStandardItemModel *model)
{
    int j=0;
    setM->setTeacherTitle(model);
    for(int i=0;i<teaInfo.length();i++)
    {
        if(teaInfo.at(i).getName()==name)
        {
            teaInfo.at(i).checkMyInfo(model,j++);
        }
    }
    return j;
}

bool adminInfo::displayStudentWithId(const QString &id, QStandardItemModel *model)
{
    int j=0;
    setM->setStudentTitle(model);
    for(int i=0;i<stuInfo.length();i++)
    {
        if(stuInfo.at(i).getId()==id)
        {
            stuInfo.at(i).checkMyInfo(model,j++);
        }
    }
    return j;
}

bool adminInfo::displayStudentWithName(const QString &name, QStandardItemModel *model)
{
    int j=0;
    setM->setStudentTitle(model);
    for(int i=0;i<stuInfo.length();i++)
    {
        if(stuInfo.at(i).getName()==name)
        {
            stuInfo.at(i).checkMyInfo(model,j++);
        }
    }
    return j;
}

void adminInfo::modifyStudentInfo(studentInfo &info, QStringList newInfo)
{

    info.setInfo(newInfo.at(0),newInfo.at(1),newInfo.at(3),newInfo.at(4),newInfo.at(5),newInfo.at(6),newInfo.at(2),info.getSelectedCourse());
}

void adminInfo::modifyTeacherInfo(teacherInfo &info, QStringList newInfo)
{
    info.setInfo(newInfo.at(0),newInfo.at(1),newInfo.at(4),newInfo.at(2),newInfo.at(3),info.getMyCourse());
}

void adminInfo::deleteStudentInfo(QString id)
{
    for(int i=0;i<stuInfo.length();i++)
    {
        if(stuInfo.at(i).getId()==id)
        {
            stuInfo.removeAt(i);
        }
    }
}

void adminInfo::deleteTeacherInfo(QString id)
{
    for(int i=0;i<teaInfo.length();i++)
    {
        if(teaInfo.at(i).getId()==id)
        {
            teaInfo.removeAt(i);
        }
    }
}

void adminInfo::deleteCourseInfo(QString id)
{
    for(int i=0;i<couInfo.length();i++)
    {
        if(couInfo.at(i).getCourseSelectId()==id)
        {
            couInfo.removeAt(i);
        }
    }
    for(int i=0;i<teaInfo.length();i++)
    {
        teaInfo[i].removeCourse(id);
    }
    for(int i=0;i<stuInfo.length();i++)
    {
        stuInfo[i].removeCourse(id);
    }
}

teacherInfo &adminInfo::findTeacherWithId(const QString &id)
{
    for(int i=0;i<teaInfo.length();i++)
    {
        if(teaInfo.at(i).getId()==id)
        {
            return teaInfo[i];
        }
    }
}

teacherInfo &adminInfo::findTeacherWithName(const QString &name)
{
    for(int i=0;i<teaInfo.length();i++)
    {
        if(teaInfo.at(i).getName()==name)
        {
            return teaInfo[i];
        }
    }
}
